package djboom.materials.display.elements {
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.core.DataCore;
	import djboom.materials.events.InstructionEvent;
	import djboom.materials.state.Font;

	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;

	import org.casalib.display.CasaBitmap;
	import org.casalib.display.CasaShape;
	import org.casalib.display.CasaSprite;
	import org.casalib.display.CasaTextField;

	import flash.display.BlendMode;
	import flash.display.GradientType;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 22.04.2010
	 * 
	 */

	public class Instruction extends CasaSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		private static const TITLE_TFORMAT:TextFormat = new TextFormat(Font.MYRIAD_PRO_COND, 20, 0x363d0a);
		private static const DROPSHADOW_FILTER:DropShadowFilter = new DropShadowFilter(1, 90, 0xffffff, 1, 0, 0, 1, 1, false, true);

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _instructionField:CasaTextField;

		private var _gradientMatrix:Matrix = new Matrix();
		private var _gradientShape:CasaShape;
		private var _instructionFieldShadow:CasaTextField;
		private var _multiplyPattern:CasaBitmap;
		private var _borderShape:CasaShape;

		private var _instructionID:int = 0;
		private var _instructionXMLList:XMLList;
		private var _instructionXML:XML;
		private var _instructionText:String;
		private var _autoButtonFlag:String;

		private var _scrollHeight:int;
		private var _cellInput:String;
		private var _autoHS:HotSpot;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function Instruction(instructionXMLList:XMLList) {
			super();

			this.x = 3;
			this.y = 82;
            			
			_instructionXMLList = instructionXMLList;
			
			_instructionXML = _instructionXMLList[_instructionID];
			_instructionText = _instructionXML.child("text").toString();
			_autoButtonFlag = _instructionXML.hasOwnProperty("auto") ? _instructionXML.child("auto").toString() : null;
			
			_instructionFieldShadow = new CasaTextField();
			_instructionFieldShadow.embedFonts = true;
			_instructionFieldShadow.selectable = false;
			_instructionFieldShadow.defaultTextFormat = TITLE_TFORMAT;
			_instructionFieldShadow.autoSize = TextFieldAutoSize.LEFT;
			_instructionFieldShadow.antiAliasType = AntiAliasType.ADVANCED;
			_instructionFieldShadow.text = _instructionText;
			_instructionFieldShadow.mouseEnabled = false;
			_instructionFieldShadow.thickness = 30;
			_instructionFieldShadow.scaleX = 1.2;
			_instructionFieldShadow.width = 820;
			_instructionFieldShadow.wordWrap = true;
			_instructionFieldShadow.multiline = true;
			_instructionFieldShadow.blendMode = BlendMode.OVERLAY;
			_instructionFieldShadow.filters = [DROPSHADOW_FILTER];			
			_instructionFieldShadow.x = 26;
			_instructionFieldShadow.y = 6;
			_instructionFieldShadow.alpha = 0.9;
			_instructionFieldShadow.styleSheet = DataCore.STYLESHEET;
			
			_instructionField = new CasaTextField();
			_instructionField.embedFonts = true;
			_instructionField.selectable = false;
			_instructionField.defaultTextFormat = TITLE_TFORMAT;
			_instructionField.autoSize = TextFieldAutoSize.LEFT;
			_instructionField.antiAliasType = AntiAliasType.ADVANCED;
			_instructionField.text = _instructionText;
			_instructionField.mouseEnabled = false;
			_instructionField.thickness = 30;
			_instructionField.scaleX = 1.2;
			_instructionField.width = 820;
			_instructionField.wordWrap = true;
			_instructionField.multiline = true;
			_instructionField.x = 26;			_instructionField.y = 6;
			_instructionField.styleSheet = DataCore.STYLESHEET;
			
			_gradientMatrix.createGradientBox(1018, _instructionField.textHeight + 18, -Math.PI / 2);

			_gradientShape = new CasaShape();
			_gradientShape.graphics.beginGradientFill(GradientType.LINEAR, [0xaec630, 0xe6ff60], [1, 1], [0, 255], _gradientMatrix);
			_gradientShape.graphics.drawRect(0, 0, 1018, _instructionField.textHeight + 18);
			_gradientShape.graphics.endFill();
			addChild(_gradientShape);
			
			_multiplyPattern = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.INSTRUCTION));
			_multiplyPattern.blendMode = BlendMode.MULTIPLY;
			_multiplyPattern.alpha = 0.3;
			_multiplyPattern.scrollRect = new Rectangle(0, 0, 1018, _gradientShape.height);
			addChild(_multiplyPattern);

			_borderShape = new CasaShape();
			_borderShape.graphics.lineStyle(1, 0xdaef67);			_borderShape.graphics.moveTo(0, 0);			_borderShape.graphics.lineTo(1018, 0);
			_borderShape.graphics.lineStyle(1, 0x90a230);
			_borderShape.graphics.moveTo(0, 1);
			_borderShape.graphics.lineTo(1018, 1);
			_borderShape.y = _gradientShape.height;
			addChild(_borderShape);
			
			addChild(_instructionFieldShadow);
			addChild(_instructionField);
			
			_scrollHeight = _gradientShape.height + 2;
			
			_instructionID++;
			
			addEventListener(Event.REMOVED_FROM_STAGE, clearContent);	
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function applyAuto(event:MouseEvent):void {
			TweenLite.to(_autoHS, 0.4, {y: _borderShape.y + 2 - _autoHS.height, ease: Cubic.easeIn, onComplete: destroyAutoHS});
			
			var instructionEvent:InstructionEvent = new InstructionEvent(InstructionEvent.AUTO);
			instructionEvent.methodName = _autoButtonFlag;
			dispatchEvent(instructionEvent);
		}

		private function destroyAutoHS():void {
			_autoHS.destroy();
			_autoHS = null;
		}

		
		private function clearContent(event:Event):void {
			_borderShape.destroy();
			_borderShape = null;
            
			_multiplyPattern.destroy();
			_multiplyPattern = null;
			
			_gradientShape.destroy();
			_gradientShape = null;
            
			_gradientMatrix = null;
			
			_instructionField.destroy();
			_instructionField = null;
			
			_instructionFieldShadow.destroy();
			_instructionFieldShadow = null;
			
			_instructionXMLList = null;
			
			_instructionXML = null;
		}

		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		public function nextInstruction():void {
			_instructionXML = _instructionXMLList[_instructionID];
			
			_instructionText = _instructionXML.child("text").toString();
			_autoButtonFlag = _instructionXML.hasOwnProperty("auto") ? _instructionXML.child("auto").toString() : null;
			_cellInput = _instructionXML.hasOwnProperty("cell") ? _instructionXML.child("cell").toString() : null;
            
			_instructionFieldShadow.text = _instructionText;
			_instructionField.text = _instructionText;
            
			_gradientShape.height = _instructionField.textHeight + 18;
			_multiplyPattern.scrollRect = new Rectangle(0, 0, 1018, _gradientShape.height);
			
			_borderShape.y = _gradientShape.height;
            
			_scrollHeight = _gradientShape.height + 2;
            
			if (_cellInput != null) {
				var instructionEvent:InstructionEvent = new InstructionEvent(InstructionEvent.LIGHT_CELL);
				instructionEvent.cellName = _cellInput;
				dispatchEvent(instructionEvent);
			}
			
			if (_autoButtonFlag != null) {
				_autoHS = new HotSpot();
				_autoHS.drawBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.AUTOBUTTON));
				_autoHS.x = 38;
				_autoHS.y = _borderShape.y + 1;
				_autoHS.addEventListener(MouseEvent.CLICK, applyAuto);
				addChildAt(_autoHS, 0);
				
				TweenLite.from(_autoHS, 0.4, {y: _borderShape.y + 2 - _autoHS.height, ease: Cubic.easeOut});
			}
			
			_instructionID++;
			
			if (_instructionID >= _instructionXMLList.length()) {
				_instructionID = 0;
			}
		}

		public function checkInputCell():Boolean {
			var instructionXML:XML = _instructionXMLList[_instructionID];
            
			var cellInput:Boolean = instructionXML.hasOwnProperty("cell") ? true : false;
			return cellInput;  
		}

		// ----------------------------------------------------------------------------
		// Методы доступа.
		// ----------------------------------------------------------------------------

		public function get scrollHeight():int {
			return _scrollHeight;
		}

		public function get instructionID():int {
			return _instructionID;
		}
	}
}
